/*
 * Copyright (c) 1994-2017, Joyoung Stock CO.LTD.
 * The FWJR Project.
 * All Rights Reserved.
 */

package com.hepinghospital.user.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import androidx.annotation.NonNull;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseDialogFragment;

import org.jetbrains.annotations.NotNull;

import java.util.Objects;


public class PayDialog extends BaseDialogFragment {
    private OnItemClickListener mListener;
    private Dialog mDialog;
    private TextView btn_confirm, tv_price;
    private ImageView btn_cancel;
    private CheckBox checkbox;


    public static PayDialog newInstance(String price) {
        Bundle args = new Bundle();
        args.putString("price", price);
        PayDialog fragment = new PayDialog();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(false);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.BOTTOM);
        }
        return mDialog;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mView = inflater.inflate( R.layout.dialog_pay, container, false);
        mView.setMinimumWidth(mScreenWidth);
        checkbox = mView.findViewById(R.id.checkbox);
        btn_confirm = mView.findViewById(R.id.btn_confirm);
        btn_cancel = mView.findViewById(R.id.btn_cancel);
        tv_price = mView.findViewById(R.id.tv_price);
        assert getArguments() != null;
        tv_price.setText(getArguments().getString("price"));
        btn_confirm.setOnClickListener( v -> {
            if (checkbox.isChecked()) {
                mListener.callback();
                dismiss();
            }
        } );
        btn_cancel.setOnClickListener( v -> mListener.onCancel() );
        return mView;
    }

    @Override
    public void onViewCreated(@NotNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Objects.requireNonNull( getDialog() ).setCanceledOnTouchOutside(false);
    }

    public interface OnItemClickListener {
        void callback();

        void onCancel();

    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


}
